package Servlet;

import Bean.Ticket;
import Service.TicketService;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 张
 * Date: 2025-03-15
 * Time: 11:54
 */
//管理员处理用户退票申请
@WebServlet("/QuitDisposeServlet")
public class QuitDisposeServlet extends HttpServlet {
    private TicketService ticketService = new TicketService();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("application/json;charset=utf8");
        String ticketId = req.getParameter("ticket_id");
        String type = req.getParameter("type");
        boolean ok = false;

        if(type.equals("pass")) {
            //通过退票申请
            //在数据库中把这个订单对应的座位号删除人然后状态修改为已退票
            ok = ticketService.quitPass(ticketId);
        } else if(type.equals("refuse")) {
            //拒绝退票申请
            //把状态改回已预定
            Ticket ticket = new Ticket();
            ticket.setTicketId(ticketId);
            ticket.setStatus(Ticket.TicketStatus.已预订);
            ok = ticketService.quitRefuse(ticket);
        }
        if(!ok) {
            req.setAttribute("status", "error");
            req.setAttribute("message", "操作失败");
            req.setAttribute("redirectUrl", "QuitsServlet?pageNo=1");
            req.setAttribute("waitSeconds", 2);
            req.getRequestDispatcher("skip.jsp").forward(req,resp);
        }
        req.getRequestDispatcher("QuitsServlet?pageNo=1").forward(req,resp);
    }
}
